<template>
  <view class="custom-navbar" :style="{ paddingTop: statusBarHeight + 'px' }">
    <view class="navbar-content">
      <view class="left" @click="handleBack">
        <w-icon v-if="showBack" name="arrow-left" size="20" />
        <slot name="left"></slot>
      </view>
      <view class="title">
        <slot>{{ title }}</slot>
      </view>
      <view class="right">
        <slot name="right"></slot>
      </view>
    </view>
  </view>
</template>

<script setup>
const props = defineProps({
  title: String,
  showBack: {
    type: Boolean,
    default: true,
  },
})

const emit = defineEmits(['back'])

const statusBarHeight = ref(0)

onMounted(() => {
  // 获取状态栏高度
  uni.getSystemInfo({
    success: (res) => {
      statusBarHeight.value = res.statusBarHeight
    },
  })
})

const handleBack = () => {
  emit('back')
  uni.navigateBack()
}
</script>

<style scoped>
.custom-navbar {
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 999;
  background-color: #fff;
}

.navbar-content {
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 15px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
}

.title {
  font-size: 16px;
  font-weight: bold;
  flex: 1;
  text-align: center;
}

.left,
.right {
  width: 60px;
}

.left {
  text-align: left;
}

.right {
  text-align: right;
}
</style>